package com.ruix.app.ui.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.ruix.app.R;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.UserMapper;
import com.ruix.app.model.User;
import com.ruix.app.model.UserInfo;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.base.BaseActivity;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/18.
 */

public class GenderChoseActivity extends BaseActivity implements View.OnClickListener{


    private int gender = 1;
    private boolean modefy;
    private View maleSateView;
    private View femaleSateView;
    public static Intent newIntent(Context context,int gender){
        Intent intent = new Intent(context,GenderChoseActivity.class);
        intent.putExtra("gender",gender);
        return intent;
    }

    public static Intent newIntentModify(Context context,int gender){
        Intent intent = new Intent(context,GenderChoseActivity.class);
        intent.putExtra("gender",gender);
        intent.putExtra("modify",1);
        return intent;
    }

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_gender_chose;
    }

    @Override
    protected void initView() {
        maleSateView =  findViewById(R.id.iv_male_state);
        femaleSateView = findViewById(R.id.iv_female_state);
        setTitle("性别");
    }

    @Override
    protected void initLocalData() {
        Intent intent =  getIntent();
        if (intent != null){
            gender = intent.getIntExtra("gender",1);
            modefy = intent.getIntExtra("modify",0)  == 1;
        }

        if (gender == 1){
            maleSateView.setVisibility(View.VISIBLE);
            femaleSateView.setVisibility(View.INVISIBLE);
        } else {
            maleSateView.setVisibility(View.INVISIBLE);
            femaleSateView.setVisibility(View.VISIBLE);
        }
    }



    @Override
    protected void initListener() {
        findViewById(R.id.rl_male_contain).setOnClickListener(this);
        findViewById(R.id.rl_female_contain).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_male_contain:
                if (modefy){
                    saveUserGender(1);
                } else {
                    goBack(1);
                }


                break;

            case R.id.rl_female_contain:
                if (modefy) {
                    saveUserGender(0);
                } else {
                    goBack(0);
                }
                break;

        }

    }

    private void goBack(int gender){
        Intent intent = new Intent();
        intent.putExtra("gender",gender);
        setResult(1002,intent);
        finish();
    }

    private void saveUserGender(final int gender){
        UserInfo userInfo = new UserInfo();
        userInfo.setSex(gender);
        showLoading();
        RequestFactory.editUserinfo(userInfo,new UserMapper(), new RXObserver<User>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(User value) {
                if (value != null && !TextUtils.isEmpty(value.getID())){
                    UserManager.sharedInstance().updateUserInfo(value);
                }
            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
            }

            @Override
            public void onComplete() {
                dismissLoading();
                finish();
            }
        });
    }



}
